package com.df.bpm.utils.businessViewer.stepsTableServices;

import com.df.bpm.dto.BusinessInformationDTO4Subset;
import com.df.bpm.dto.BusinessStepDTO;
import com.df.bpm.entity.BusinessInformation;
import com.df.bpm.entity.BusinessLocations;
import com.df.bpm.utils.businessViewer.Main;
import com.df.common.utils.CommonUtils;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

public class StepService {


    /**
     * 获取业务步骤
     * @param superior 步骤所属业务的ID
     * @param groupID   步骤分组id
     * @return  业务步骤
     */
    public Map<Integer, BusinessStepDTO> getSteps(Integer superior, Integer groupID) {
        if(CommonUtils.varIsBlank(superior)){
            superior=-1;
        }
        if(CommonUtils.varIsBlank(groupID)){
            groupID=-1;
        }
        RestTemplate restTemplate = new RestTemplate();
        String url=Main.server+"businessInformation/getSteps?groupID="+groupID
                +"&superior="+superior;
        ResponseEntity<Map<Integer, BusinessStepDTO>> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                new ParameterizedTypeReference<Map<Integer, BusinessStepDTO>>() {}
        );
        return response.getBody();
    }

    /**
     * 更新步骤信息
     * @param iStep 业务信息
     * @return 影响的行数
     */
    public Integer updateStep(BusinessInformation iStep,BusinessLocations location) {
        if(iStep.getBusinessDesc()!=null
                && !iStep.getBusinessDesc().isEmpty()
                && iStep.getEntityID()!=null
        ){
            Map<Integer,BusinessStepDTO> steps=new HashMap<>();
            steps.put(location.getSequence(), new BusinessStepDTO(location, (BusinessInformationDTO4Subset) iStep));
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+"businessInformation/update";
            HttpEntity<Map<Integer,BusinessStepDTO>> request = new HttpEntity<>(steps, null);
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.POST,
                    request, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }

    /**
     * 删除步骤信息
     * @param location 步骤信息
     * @return 影响的行数
     */
    public Integer deleteStep(BusinessLocations location) {
        if(location!=null && location.getBusinessLocationsIndex()!=null){
            RestTemplate restTemplate = new RestTemplate();
            String url=Main.server+"businessInformation/deleteLocation?businessLocationsIndex="
                    +location.getBusinessLocationsIndex();
            ResponseEntity<?> response = restTemplate.exchange(
                    url,
                    HttpMethod.GET,
                    null, // 或者使用 HttpEntity 来添加请求头等信息
                    Integer.class
            );
            return (Integer) response.getBody();
        }
        return null;
    }

    /**
     * 修改步骤顺序
     * <P>head to tail</P>
     * @param headSequence 要修改的原始序号
     * @param tailSequence  目标位置序号
     * @param superior  父级业务ID
     * @param groupID   分组ID
     */
    public void updateStepSequence(int headSequence, int tailSequence, Integer superior, Integer groupID) {
        RestTemplate restTemplate = new RestTemplate();
        String url=Main.server+"businessInformation/updateSequence?"
                +"headSequence="+headSequence
                +"&tailSequence="+tailSequence
                +"&superior="+superior
                +"&groupID="+groupID;
        restTemplate.exchange(
                url,
                HttpMethod.GET,
                null, // 或者使用 HttpEntity 来添加请求头等信息
                Integer.class
        );
    }
}
